<template>
	<view class="content">
		<view class="logo">
			A
		</view>
		<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="confirm-btn" v-show="btnShow" :disabled="disabled1">微信一键登录</button>
		<view class="login-hint" v-show="btnShow1">请完成微信授权后继续使用</view>
		<button open-type="getUserInfo" class="confirm-btn" @getuserinfo="goLoginBtn" withCredentials="true"  v-show="btnShow1" :disabled="disabled2">授权微信信息</button>
		
	</view>
</template>

<script>
	import tuiModal from "@/components/modal/modal" //弹窗
	import {weChatGetOpenId,getPhoneNumber,registered} from '@/api/index.js';
	export default {
		components: {
			tuiModal,
		},
		data() {
			return {
				logining: false,//按钮是否禁止
				phone:"",
				btnShow:true,
				btnShow1:false,
				disabled1:false,//第一个按钮是否禁止
				disabled2:false,
				isCanUse: uni.getStorageSync('isCanUse')||true,//记录是否是第一次授权，默认为true
				encryptedData: "", //加密数据
				iv: "",
				userInfo: {},
				openId:"",
				parentUserId:"",//推荐人id
			}
		},
		onLoad(e) {
			let _that =this
			_that.parentUserId = e.userId
			console.log("分享用户id"+_that.parentUserId)
			let token=uni.getStorageSync('token');
			if(token!==''){
				uni.switchTab({
					url: "index/index"
				});
			}
		},
		methods: {
			//获取手机号
			getPhoneNumber(e){
				console.log(e)
				let that=this;
				let encryptedData=e.detail.encryptedData;
				let iv=e.detail.iv;
				that.disabled1=true;
				wx.checkSession({
			　　　　	success: function(res){
						wx.login({
							success(res){
								let code=res.code;
								console.log(res.code)
								if(code) {
									weChatGetOpenId(code).then(re =>{
										console.log(re)
										that.openId=re.openid;
										let userInfo=re.userInfo;
										if(userInfo!==null){
											uni.setStorageSync('userInfo',userInfo);
											let token=re.userInfo.token;
											uni.setStorageSync('token',token);
											that.disabled1=false;
											that.$api.msg('登录成功');
											setTimeout(function(){
												uni.switchTab({													url: "index/index"												});
											},300)
										}else{
											let sessionKey=re.session_key;
											getPhoneNumber(encryptedData,iv,sessionKey).then(re =>{
												that.phone=JSON.parse(re).phoneNumber;
												that.btnShow=false;
												that.btnShow1=true;
												that.disabled1=false;
											}).catch( re=>{
												that.disabled1=false;
											}); 
										}
										
									}).catch( re=>{
										console.log(re);
										that.disabled1=false;
									}); 
								}
							},
							fail(re) {
								console.log(re);
								that.disabled1=false;
							}
						})
			　　　　	},
					fail: function(res){
						wx.login({
							success(res){
								let code=res.code;
								if(code) {
									weChatGetOpenId(code).then(re =>{
										that.openId=re.openid;
										let userInfo=re.userInfo;
										if(userInfo!==null){
											uni.setStorageSync('userInfo',userInfo);
											let token=re.userInfo.token;
											uni.setStorageSync('token',token);
											that.disabled1=false;
											that.btnShow1=true;
											that.$api.msg('登录成功');
											setTimeout(function(){
												uni.switchTab({
													url: "index/index"
												});
											},300)
										}else{
											let sessionKey=re.session_key;
											getPhoneNumber(encryptedData,iv,sessionKey).then(re =>{
												that.phone=JSON.parse(re).phoneNumber;
												that.btnShow=false;
												that.btnShow1=true;
											}).catch( re=>{
												console.log(re);
											}); 
										}
									}).catch( re=>{
										that.btnShow=false;
										console.log(re);
									}); 
								}
							},
							fail(re) {
								that.btnShow=false;
								console.log(re);
							}
						})
			　　　　}
			　　})
			},
			//登录操作
			goLoginBtn(){
				let that=this;
				let phone=this.phone;
				let openid=this.openId;
				that.btnShow1=true;
				that.disabled2=true;
				uni.getUserInfo({//获取用户信息
					success: (re) => {
						console.log(re)
						let nickName=re.userInfo.nickName;
						let avatarUrl=re.userInfo.avatarUrl;
						let gender=re.userInfo.gender;
						// let parentUserId="";
						let unionId="";
						//注册账号
						registered(avatarUrl,gender,nickName,openid,that.parentUserId,phone,unionId).then(re =>{
							let userInfo=re;
							uni.setStorageSync('userInfo',userInfo);
							let token=userInfo.token;
							uni.setStorageSync('token',token);
							that.disabled1=false;
							that.$api.msg('登录成功');
							that.btnShow1=false;
							uni.switchTab({
								url: "index/index"
							});
						}).catch( re=>{
							that.disabled2=false;
							console.log(re);
						}); 
					},
					fail(re) {
						that.disabled2=false;
						console.log(re);
					}
				}) 
				
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding-top: 140rpx
	}

	.logo {
		width: 144rpx;
		height: 144rpx;
		border-radius: 144rpx;
		background: #FF203B;
		color: #fff;
		font-size: 120rpx;
		line-height: 144rpx;
		text-align: center;
		margin: 0 auto;
	}

	.login-btn {
		margin-top: 140rpx;
	}

	.next {
		margin: 0 76rpx;
	}

	.next .next2 {
		background: url("../static/images/band-card.png") no-repeat;
		background-size: 100% 100%;
		color: #fff;
		width: 600rpx;
		height: 80rpx;
		margin: 130rpx auto 0;
		line-height: 80rpx;
		text-align: center;
		font-size: 28rpx;
	}

	.next .next3 {
		background: url("../static/images/band-card.png") no-repeat;
		background-size: 100% 100%;
		color: #fff;
		width: 600rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 28rpx;
		position: fixed;
		bottom: 60rpx;
	}

	.choose-staff {
		font-family: PingFangSC-regular;
		text-align: left;
		font-size: 13px;
		color: rgba(106, 106, 106, 1);
	}

	.cu-dialog {
		height: 764rpx;
	}

	.qxmd {
		width: 50rpx;
		height: 50rpx;
		color: rgba(189, 189, 189, 1);
		font-size: 50rpx;
		flex: 1;
		display: flex;
		justify-content: flex-end;
		padding: 20rpx 20rpx 0 0rpx;
	}

	.bind {
		font-size: 40rpx;
		color: #101010;
	}

	.tips {
		font-size: 28rpx;
		color: #999999;
		margin-top: 28rpx;
	}

	.send-code {
		background: #fff;
		color: #FF203B;
	}

	.cu-form-group input {
		text-align: left;
	}

	.form-list {
		padding: 80rpx;
	}

	.top {
		width: 100%;
	}

	.close {
		width: 100%;
		text-align: right;
		justify-content: end;
		display: flex;
		align-items: center;
	}

	.cu-form-group {
		border-top: 0;
		border-bottom: 0.5px solid #eee;
	}
	.confirm-btn{
		width: 670rpx;
		margin:140rpx auto 0;
		background: $uni-bg-color-green;
		color:$font-color-white;
	}
	.login-hint{
		text-align: center;
		margin-top: 30rpx;
	}
</style>
